package com.ruoyi.market.service.warning;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.market.model.param.WarnInfoParam;
import com.ruoyi.market.model.result.WarnInfoResult;
import com.ruoyi.market.service.IWarningService;
import org.springframework.stereotype.Service;

@Service
public class WoLianSmsMarketWarningService implements IWarningService {

    @Override
    public WarnInfoResult handleWarnInfo(WarnInfoParam warnInfoParam) {

        WarnInfoResult warnInfoResult = new WarnInfoResult();
        warnInfoResult.setTitle("短信营销（沃联）");
        try {

            String requestUrl = "http://49.234.19.14:9511/api/balance";
            String spId = "771532";
            String password = "f1db78924735c8119d413d82e5611a7f";

            requestUrl = StrUtil.format("{}?sp_id={}&password={}", requestUrl, spId, password);
            String body = HttpUtil.get(requestUrl);

            JSONObject result = JSON.parseObject(body);
            Integer code = result.getInteger("code");
            String msg = result.getString("msg");

            if (StrUtil.equals(String.valueOf(code), "0")){
                JSONObject data = result.getJSONObject("data");
                String balance = data.getString("balance");

                warnInfoResult.setMessage(balance);
                return warnInfoResult;
            }
            warnInfoResult.setMessage(code + "|" + msg);

        }catch (Exception exception){
            exception.printStackTrace();
            warnInfoResult.setMessage(exception.getMessage());
        }
        return warnInfoResult;
    }
}